package shopping

import (
	"time"
	"strconv"
	"strings"
	"dhfshop/models/shopping"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

// 配货通知管理
type ShipController struct {
	base.BaseController
}
//查询列表
func (this *ShipController) ShipList() {
	curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	if curUserId == 0 {
		this.ServeError("user is not login")
	    return
	}
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	if curRoleId == 1 {
	    query["member_id"] = strconv.FormatInt(curUserId, 10)
	}else if curRoleId == 2 {
	    query["partner_id"] = strconv.FormatInt(companyId, 10)
		/*
		if this.CurrentUser != nil {
	        if this.CurrentUser.PartByUser {
		       query["user_id"] = strconv.FormatInt(curUserId, 10)
		    }
	    }
		*/
	}else{
	
	}
	var allowStoreId []int64
	if this.CurrentUser != nil {
	    allowStoreId = this.CurrentUser.AllowStoreId
	}
	//beego.Debug("allowStoreId:", allowStoreId)
	l, cnt, err := shopping.GetAllShipNotifys(allowStoreId,query,fields,order_by,offset,limit)
	if err != nil {
	    this.ServeError(err.Error())
		return	
	}
	r := struct {
	    TotalCount int64                  `json:"total_count"`
	    Orders     []*shopping.ShipNotify `json:"ship_notify"`
	}{TotalCount: cnt, Orders: l}
	this.Data["json"] = r
	this.ServeJSON()
}

//订单拆分配送单
func (this *ShipController) Order2Delivery() {
	//curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	//companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	if curUserId == 0 {
		this.ServeError("user is not login")
	    return
	}
	v := struct {
	    StoreId   int64    `form:"store_id"`
		OrderId   string   `form:"order_id"`
		GroupIds  string   `form:"group_ids"`
	}{}     
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	orderId, err := strconv.ParseInt(v.OrderId, 10, 64)
	if err != nil {
	    this.ServeError(err.Error())
		return	
	}
	orders, err := shopping.GetOrderById(orderId)
	if err != nil {
	    this.ServeError(err.Error())
		return	
	}
	if len(v.GroupIds) > 2 {
	    for _, ids := range strings.Split(v.GroupIds, ";") {
			//创建配送单
			for _, id := range strings.Split(ids, ","){
			  //创建配送单商品列表
			  beego.Debug(orders.OrderId, id)
			}
	    }
	}
	r := struct {
	    ShipNotifyIds []string `json:"ship_notify_ids"`
	}{}
	this.Data["json"] = r
	this.ServeJSON()
}

//编辑
func (this *ShipController) Put() {
	var v shopping.ShipNotify
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.ShipNotifyId, "delivery_notify_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	excludeArr := []string{"delivery_notify_id","partner_id","store_id"}
	updateFields, _:= this.GetUpdateFields(v, "", "form", excludeArr)
    if err := shopping.UpdateShipNotifyById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
	   ShipNotifyId int64     `json:"delivery_notify_id"`
	   Modified     time.Time `json:"modified"`
	}{ShipNotifyId:v.ShipNotifyId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//删除
func (this *ShipController) Delete() {
    companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
    //curUserId := this.Ctx.Input.GetData("curUserId").(int)
	v := struct {
		CompanyId    int64 `form:"-"  valid:"Required;Min(1)"`
	    ShipNotifyId int64 `form:"delivery_notify_id" valid:"Required"`
	}{CompanyId: companyId} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	if err := shopping.DeleteShipNotify(v.ShipNotifyId); err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		ShipNotifyId int64     `json:"delivery_notify_id"`
		Modified     time.Time `json:"modified"`
	}{ShipNotifyId:v.ShipNotifyId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}
